package com.edu.service.impl;

import com.edu.bean.User;
import com.edu.mapper.UserMapper;
import com.edu.service.UserService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.sql.Connection;
import java.util.List;



@Service
public class UserServiceImpl implements UserService {

    @Resource
    private UserMapper userMapper;
    @Override
    public List<User> getAllUser() {
        return userMapper.selectAllUser();
    }

    @Override
    public int addUser(User user) {
        return userMapper.insertUser(user);
    }


    @Override
//    @Transactional(propagation = Propagation.REQUIRED,isolation = Isolation.DEFAULT,timeout = 5)
//    @Transactional(readOnly = true)
    public int transferMoney(int id1, int id2, double money) {
       User user1 = userMapper.selectById(id1);
       User user2 = userMapper.selectById(id2);

       if(user2 == null){
           return -2;
       }
       if(user1.getBalance() < money){
           return -1;
       }
       int r1 = userMapper.decreaseBalance(id1,money);
        System.out.println(1);
        try {
            Thread.sleep(4000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        int r2 = userMapper.increaseBalance(id2,money);

       return r1 ==1 && r2 == 1 ? 1 : 0;

    }
}
